import matplotlib.pyplot as plt
from sklearn.datasets import make_blobs
import numpy as np
from sklearn.cluster import AgglomerativeClustering
import scipy.cluster.hierarchy as sch

plt.figure(figsize=(20, 4))

n_samples = 1500
x,y = make_blobs(n_samples=n_samples, random_state=170)

#points = scipy.randn(20,4)
plt.subplot(151)
plt.scatter(x[:,0],x[:,1], c=y)
plt.title("original")


import scipy.cluster.hierarchy as sch
dis_mat = sch.distance.pdist(x,'euclidean')
z = sch.linkage(dis_mat, method='complete')
#sch.dendrogram(z)
#plt.savefig('ceng.png')

cluster = sch.fcluster(z,3, criterion='maxclust')
plt.subplot(152)
plt.scatter(x[:,0],x[:,1], c=cluster)
plt.title("Hierarchy")


from sklearn import metrics
from sklearn.cluster import DBSCAN
from sklearn.cluster import KMeans

n_samples = 1500
x, y = make_blobs(n_samples=n_samples, random_state=170)
trans = [[0.6083, -0.6366],[-0.4088,0.8525]]
X = np.dot(x, trans)


db = DBSCAN(eps=0.3, min_samples=10).fit(X)
labels = db.labels_
plt.subplot(153)
plt.scatter(X[:,0],X[:,1], c=labels)
plt.title("DBSCAN")




#centers = [[1, 1], [-1, -1], [1, -1]]
#X, labels_true = make_blobs(n_samples=300, 
#                            centers=centers, 
#                            cluster_std=0.5,
#                            random_state=0)

from sklearn.cluster import AffinityPropagation
af = AffinityPropagation(preference=-50).fit(X)
af_label = af.labels_
#print(af_label)

plt.subplot(154)
plt.scatter(X[:,0],X[:,1], c=af_label)
plt.title("AF")

y_pred_varied = KMeans(n_clusters=3, random_state=170).fit_predict(X)
plt.subplot(155)
plt.scatter(X[:,0],X[:,1], c=y_pred_varied)
plt.title("Predict Varied")



plt.savefig('cluser2.png')

